using AdmissionQA.Application.Dtos.CampusLife;
using AdmissionQA.Application.ServiceInterfaces;
using Microsoft.AspNetCore.Mvc;

namespace AdmissionQA.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class CampusLifesController(ICampusLifeServices campusLifeServices) : ControllerBase
{
    private readonly ICampusLifeServices _campusLifeServices = campusLifeServices;

    [HttpGet("all")]
    public async Task<IActionResult> GetAll()
    {
        var result = await _campusLifeServices.GetAllCampusLivesAsync();
        return Ok(result);
    }

    [HttpGet("paged/{pageIndex}/{pageSize}")]
    public async Task<IActionResult> GetPaged(int pageIndex = 0, int pageSize = 10)
    {
        var result = await _campusLifeServices.GetPagedCampusLivesAsync(pageIndex, pageSize);
        return Ok(result);
    }

    [HttpGet("search")]
    public async Task<IActionResult> Search(string category)
    {
        var result = await _campusLifeServices.SearchCampusLivesByCategoryAsync(category);
        return Ok(result);
    }

    [HttpGet("count")]
    public async Task<IActionResult> GetCount()
    {
        var result = await _campusLifeServices.GetCampusLifeCountAsync();
        return Ok(result);
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var result = await _campusLifeServices.GetCampusLifeByIdAsync(id);
        return Ok(result);
    }

    [HttpPost("one")]
    public async Task<IActionResult> Create(CampusLifeDto campusLife)
    {
        var result = await _campusLifeServices.CreateCampusLifeAsync(campusLife);
        return Ok(result);
    }

    [HttpPost("many")]
    public async Task<IActionResult> CreateMany(CampusLivesDto campusLives)
    {
        var result = await _campusLifeServices.CreateCampusLivesAsync(campusLives);
        return Ok(result);
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, CampusLifeDto campusLife)
    {
        var result = await _campusLifeServices.UpdateCampusLifeAsync(id, campusLife);
        return Ok(result);
    }

    [HttpDelete("one/{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var result = await _campusLifeServices.DeleteCampusLifeAsync(id);
        return Ok(result);
    }

    [HttpDelete("many")]
    public async Task<IActionResult> DeleteMany(IEnumerable<Guid> ids)
    {
        var result = await _campusLifeServices.DeleteCampusLivesAsync(ids);
        return Ok(result);
    }

    /// <summary>
    /// 语义搜索 - 基于向量相似度的校园生活信息搜索
    /// </summary>
    /// <param name="query">用户查询文本</param>
    /// <param name="topK">返回最相似的前K个结果，默认5个</param>
    /// <returns>最相关的校园生活信息结果</returns>
    [HttpGet("semantic-search")]
    public async Task<IActionResult> SemanticSearch([FromQuery] string query, [FromQuery] int topK = 5)
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return BadRequest("查询文本不能为空");
        }
        
        var result = await _campusLifeServices.SearchCampusLifesByEmbeddingAsync(query, topK);
        return Ok(result);
    }

    /// <summary>
    /// 智能校园生活推荐 - 返回最相关的校园生活信息
    /// </summary>
    /// <param name="query">用户查询</param>
    /// <returns>最相关的校园生活信息</returns>
    [HttpGet("smart-recommend")]
    public async Task<IActionResult> SmartRecommend([FromQuery] string query)
    {
        if (string.IsNullOrWhiteSpace(query))
        {
            return BadRequest("查询文本不能为空");
        }

        var result = await _campusLifeServices.SearchCampusLifesByEmbeddingAsync(query, 1);
        if (result.Data != null && result.Data.Any())
        {
            var bestMatch = result.Data.First();
            return Ok(new
            {
                Query = query,
                Category = bestMatch.Category,
                Description = bestMatch.Description,
                Confidence = "高",
                Source = "智能校园生活推荐系统"
            });
        }

        return Ok(new
        {
            Query = query,
            Message = "抱歉，没有找到相关的校园生活信息。请尝试换个关键词搜索，或者联系学生处获取更多校园生活详情。",
            Confidence = "低",
            Source = "智能校园生活推荐系统"
        });
    }
}
